﻿package com.swfmessenger.client
{

import flash.display.Sprite;
import flash.text.TextField;
import flash.errors.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.utils.Dictionary;
import flash.system.Security;
import flash.system.SecurityDomain;

import com.swfmessenger.net.URLLoaderEx;

public class SWFMessenger extends Sprite
{
	
	public function SWFMessenger() {
		
		var HOST:String = "http://gateway.messenger.hotmail.com/gateway/gateway.dll?Action=open&Server=NS&IP="+escape("messenger.hotmail.com");
		//HOST = "http://localhost";
		//HOST = "http://www.google.com.br";
		HOST = "http://gateway.messenger.hotmail.com";///gateway/gateway.dll?Action=open&Server=NS&IP="+escape("messenger.hotmail.com");
		//Security.allowDomain("http://gw.msnmessenger.akadns.net");
		//Security.allowInsecureDomain("http://gateway.messenger.hotmail.com/");

		var req:URLRequest = new URLRequest(HOST);
		//req.method = URLRequestMethod.GET;
		
		var dt:String = "";
		dt += "POST /gateway/gateway.dll?Action=open&Server=NS&IP="+escape("messenger.hotmail.com");
		//dt += "POST /8arte/head.php";///gateway/gateway.dll?Action=open&Server=NS&IP="+escape("messenger.hotmail.com");
		
		dt += " HTTP/1.1\r\n";
		
		dt +="Accept: *"+"/*\r\n";
		dt +="Accept-Language: en-us\r\n";
		dt +="Accept-Encoding: gzip, deflate\r\n";
		dt +="User-Agent: MSMSGS\r\n";
		dt +="Host: gateway.messenger.hotmail.com\r\n";
		dt +="Proxy-Connection: Keep-Alive\r\n";
		dt +="Connection: Keep-Alive\r\n";
		dt +="Pragma: no-cache\r\n";
		dt +="Content-Type: application/x-msn-messenger\r\n";
		
		dt +="Content-Length: 18\r\n";
		dt +="\r\n";
		dt +="VER 5 MSNP8 CVR0\r\n";
		//
		//dt = "GET http://localhost:80/labs/index.php HTTP/1.1\r\n";
		//dt += "Host: localhost\r\n";
		//dt +="Accept: *"+"/*\r\n";
		//dt +="Pragma: no-cache\r\n";
		//dt +="Content-Length: 49\r\n";
		//dt +="Proxy-Connection: Keep-Alive\r\n";
		//dt +="Connection: Keep-Alive\r\n";
		//dt +="Pragma: no-cache\r\n";
		//dt += "\r\n";
		//dt += "?hl=pt-BR&q=pirulito&btnG=Pesquisa+Google&meta=\r\n";//49

		req.data = dt;
		
		var loader:URLLoaderEx = new URLLoaderEx();
		//loader.dataFormat = URLLoaderDataFormat.BINARY;
		loader.addEventListener(Event.COMPLETE, loadComp);
		loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, getHeader);
		
		loader.load(req);
		
	}
	
	function closeHandler(evt:Event):void {
		tracer.appendText(evt+"\n");
	}
	function connectHandler(evt:Event):void {
		tracer.appendText(evt+"\n");
	}
	function ioErrorHandler(evt:Event):void {
		tracer.appendText(evt+"\n");
	}
	function securityErrorHandler(evt:Event):void {
		tracer.appendText(evt+"\n");
	}
	function socketDataHandler(evt:Event):void {
		tracer.appendText(evt+"\n");
	}
	function loadComp(e:Event):void {
		//headers stroed as name-value(hash map)
	
		var rh:String = URLLoaderEx(e.target).httpHeadersString;
	
		var str:String = "";
	
		//for(var p:String in rh) str+= p + ":" + rh[p] + "\n";
		//for each(var p:Object in rh) str+= p+"\n";// + ":" + rh[p] + "\n";
	
		trace("Response Headers: \n" + rh + "\n\n");
		tracer.appendText("Response Headers: \n" + rh + "\n\n");
		trace(URLLoaderEx(e.target).data);
		info.appendText(URLLoaderEx(e.target).data + "\n");
	
		//data property holds the content
	
		//trace("Body Content:\n" + HTTPURLLoader(e.target).data + "\n\n");
		tracer.appendText("Fecha conexão\n");
		URLLoaderEx(e.target).close();
	}
	function getHeader(e:HTTPStatusEvent):void {
		var st:HTTPStatusEvent = HTTPStatusEvent(e);
		tracer.appendText(e.status+"\n");
	}

	
}
	
}